//
//  NavPageVCViewController.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/25.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "NavPageVCViewController.h"

@interface NavPageVCViewController ()

@property (nonatomic, strong) id currentVC;

@end

@implementation NavPageVCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (NSMutableArray<UIViewController *> *)dataArray {
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    
    return _dataArray;
}
    

- (void)setupWithNavTitles:(NSArray *)navTitles PageVcClass:(NSArray *)pageArray {
    
    // 分栏导航条 nav 初始化
    self.navBar = [[WPYNavBar alloc] initWithFrame:CGRectMake(0,kNavigationBarHeight, kScreenWidth, 44) titleColor:kContentColor andSelectTitleColor:kThemeColor];
    self.navBar.delegate = self;
    self.navBar.titleFont = Font(14);
    [self.navBar AddNavBarArray:navTitles isBtnLine:YES btnLineColor:kThemeColor];
    
    [self.view addSubview:self.navBar];
    
    // pageVC
    
    NSDictionary *option = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
    self.pageVC = [[UIPageViewController alloc]
                   initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                   navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                   options:option];
    
    self.pageVC.dataSource = self;
    self.pageVC.delegate = self;
    
    for (BaseVC * pageVC in pageArray) {
        
        pageVC.superVC = self;
        
        [self.dataArray addObject:pageVC];
    }
    
    
    [self.pageVC setViewControllers:@[self.dataArray[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self.view addSubview:self.pageVC.view];
    
    [self.pageVC.view mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.navBar.mas_bottom);
        make.leading.equalTo(self.view.mas_leading);
        make.trailing.equalTo(self.view.mas_trailing);
        
        if (self.isHiddenBootomBar) {
            
            make.bottom.equalTo(self.view.mas_bottom).offset(-kBottomHeight);
        } else {
            if(self.hidesBottomBarWhenPushed){
                make.bottom.equalTo(self.view.mas_bottom);
            }else{
            make.bottom.equalTo(self.view.mas_bottom).offset(-kTableBarHeight);
            }
        }
    }];
    
    self.currentVC = self.dataArray[0];
}

//用于初始化  显示第几个vc
- (void)setCurrentVCWithNum:(NSInteger)num {
    
    [self.pageVC setViewControllers:@[self.dataArray[num]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    self.currentVC = self.dataArray[num];
    self.navBar.selectIndex = num;
}

#pragma mark - WPYNavBarDelegate
- (void)selectChangetoViewNum:(NSInteger)num {
    
    [self.pageVC setViewControllers:@[self.dataArray[num]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    self.currentVC = self.dataArray[num];
    
}
#pragma mark - UIPageViewControllerDataSource


/**
 *  @brief 点击或滑动 UIPageViewController 左侧边缘时触发
 *
 *  @param pageViewController 翻页控制器
 *  @param viewController     当前控制器
 *
 *  @return 返回前一个视图控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController
               viewControllerBeforeViewController:(UIViewController *)viewController {
    // 计算当前 viewController 数据在数组中的下标
    NSUInteger index = [self.dataArray indexOfObject:viewController];
    
    // index 为 0 表示已经翻到最前页
    if (index == 0 ||index == NSNotFound) {
        return  nil;
    }
    
    // 下标自减
    index --;
    
    return self.dataArray[index];
}


/**
 *  @brief 点击或滑动 UIPageViewController 右侧边缘时触发
 *
 *  @param pageViewController 翻页控制器
 *  @param viewController     当前控制器
 *
 *  @return 返回下一个视图控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController
                viewControllerAfterViewController:(UIViewController *)viewController {
    // 计算当前 viewController 数据在数组中的下标
    NSUInteger index = [self.dataArray indexOfObject:viewController];
    // index为数组最末表示已经翻至最后页
    if (index == NSNotFound ||
        index == (self.dataArray.count - 1)) {
        return nil;
    }
    
    // 下标自增
    index ++;
    return self.dataArray[index];
}


/**
 *  @brief 转场动画即将开始
 *
 *  @param pageViewController     翻页控制器
 *  @param pendingViewControllers 即将展示的控制器
 */
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    
}

/**
 *  @brief 该方法会在 UIPageViewController 翻页效果出发之后，尚未完成时执行
 *
 *  @param pageViewController      翻页控制器
 *  @param finished                动画完成
 *  @param previousViewControllers 前一个控制器(非当前)
 *  @param completed               转场动画执行完
 */
- (void)pageViewController:(UIPageViewController *)pageViewController
        didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers
       transitionCompleted:(BOOL)completed {
    
    NSUInteger index = [self.dataArray indexOfObject:self.pageVC.viewControllers.firstObject];
    self.navBar.selectIndex = index;
    
    self.currentVC = self.dataArray[index];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
